package com.zxgj.datadirect.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxgj.common.annotation.Log;
import com.zxgj.common.core.controller.BaseController;
import com.zxgj.common.core.domain.AjaxResult;
import com.zxgj.common.core.page.TableDataInfo;
import com.zxgj.common.enums.BusinessType;
import com.zxgj.datadirect.entity.DataDirectTodo;
import com.zxgj.datadirect.service.DataDirectTodoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 我的待办管理Controller
 * 
 * @author ruoyi
 */
@Api(tags = "我的待办管理")
@RestController
@RequestMapping("/datadirect/todo")
@RequiredArgsConstructor
public class DataDirectTodoController extends BaseController {

    private final DataDirectTodoService todoService;

    /**
     * 分页查询待办列表
     */
    @ApiOperation("分页查询待办列表")
    @PreAuthorize("@ss.hasPermi('datadirect:todo:list')")
    @GetMapping("/page")
    public TableDataInfo getTodoPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") int pageSize,
            @ApiParam("经办人") @RequestParam(required = false) String assignee,
            @ApiParam("状态") @RequestParam(required = false) String status) {
        
        startPage();
        Page<DataDirectTodo> page = new Page<>(pageNum, pageSize);
        Page<DataDirectTodo> result = todoService.getTodoPage(page, assignee, status);
        return getDataTable(result.getRecords());
    }

    /**
     * 查看待办详情
     */
    @ApiOperation("查看待办详情")
    @PreAuthorize("@ss.hasPermi('datadirect:todo:query')")
    @GetMapping("/{id}")
    public AjaxResult getTodoDetail(@ApiParam("待办ID") @PathVariable Long id) {
        DataDirectTodo todo = todoService.getTodoDetail(id);
        return success(todo);
    }

    /**
     * 批量标记待办已读
     */
    @ApiOperation("批量标记待办已读")
    @PreAuthorize("@ss.hasPermi('datadirect:todo:read')")
    @Log(title = "标记待办已读", businessType = BusinessType.UPDATE)
    @PostMapping("/markRead")
    public AjaxResult markTodoRead(
            @ApiParam("申请单号列表") @RequestBody List<String> applyNos) {
        
        Long userId = getUserId();
        int successCount = todoService.markTodoRead(applyNos, userId);
        return success("标记成功");
    }

    /**
     * 统计待办数量
     */
    @ApiOperation("统计待办数量")
    @PreAuthorize("@ss.hasPermi('datadirect:todo:count')")
    @GetMapping("/count")
    public AjaxResult countTodo(@ApiParam("经办人") @RequestParam String assignee) {
        int count = todoService.countTodo(assignee);
        return success(count);
    }

    /**
     * 获取我的待办列表（不分页）
     */
    @ApiOperation("获取我的待办列表")
    @PreAuthorize("@ss.hasPermi('datadirect:todo:list')")
    @GetMapping("/my")
    public AjaxResult getMyTodoList(@ApiParam("经办人") @RequestParam String assignee) {
        Page<DataDirectTodo> page = new Page<>(1, 100); // 获取前100条
        Page<DataDirectTodo> result = todoService.getTodoPage(page, assignee, null);
        return success(result.getRecords());
    }
}
